<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Company;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CompanyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Company(), function (Grid $grid) {
            $grid->disablePerPages();

            if (!Admin::user()->can('company-batch-delete')) {
                $grid->disableRowSelector();
                $grid->disableBatchDelete();
            }
            $grid->scrollbarX();
            $grid->withBorder();
            $grid->addTableClass(['table-text-center']);

            $grid->disableViewButton();
            $grid->enableDialogCreate();

            $grid->disableEditButton();
            $grid->showQuickEditButton();

            $grid->column('id')->sortable();
            $grid->column('company_name');
            $grid->column('company_address');
            $grid->column('created_at')->sortable();
            // $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('id', 'desc');

            /**
             * 按钮控制.
             */
            // @permissions
            if (!Admin::user()->can('company-create')) {
                $grid->disableCreateButton();
                // $grid->disableQuickCreateButton();
            }

            // @permissions
            if ((!Admin::user()->can('company-update'))) {
                $grid->disableEditButton();
                // $grid->disableQuickEditButton();
            }
            // @permissions
            if (!Admin::user()->can('company-delete')) {
                $grid->disableDeleteButton();
            }

            // @permissions
            if (!Admin::user()->can('company-view')) {
                $grid->disableViewButton();
            }


            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('company_name');
                $filter->date('created_at', '创建时间');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Company(), function (Show $show) {
            $show->field('id');
            $show->field('company_name');
            $show->field('company_address');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Company(), function (Form $form) {

            $form->disableViewButton();

            $form->display('id');
            $form->text('company_name')->required();
            $form->text('company_address')->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
